﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace DanhMuc
{
    public partial class frmHocVan : Form
    {
        public frmHocVan()
        {
            InitializeComponent();
        }

        enum ActionMode { NEW, EDIT, DEL }

        ActionMode actMode = ActionMode.NEW;
        HocVanCtr Ctr = new HocVanCtr();
        HocVanInfo Obj = new HocVanInfo();

        private void frmHocVan_Load(object sender, EventArgs e)
        {
            InitGrid();
        }

        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[3];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Học Vấn";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "HocVanID";
            //Cols[i].OptionsColumn.ReadOnly = true;

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên Học Vấn";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TenHocVan";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Sử Dụng";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "Active";


            this.gridView1.Columns.AddRange(Cols);

            //GridColumn gridColumn2 = new GridColumn();
            //this.gridView1.Columns.AddRange(Cols);
            //this.gridView1.GridControl = this.gridControl1;
            //this.gridView1.Name = "gridView1";
            //this.gridView1.OptionsView.ColumnAutoWidth = false;
            //this.gridView1.IndicatorWidth = 30;
            this.gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            //ColumnFilterInfo            
            this.gridView1.OptionsView.ShowGroupPanel = false;
            this.gridView1.OptionsView.ShowAutoFilterRow = true;
            this.gridControl1.DataSource = Ctr.GetList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dtbHocVan = (DataTable)gridControl1.DataSource;
            foreach (DataRow Rows in dtbHocVan.Rows)
            {
                if (Rows.RowState == DataRowState.Added)
                {
                    Obj = new HocVanInfo();
                    Obj.HocVanID = Rows["HocVanID"].ToString();
                    Obj.TenHocVan = Rows["TenHocVan"].ToString() == null ? string.Empty : Rows["TenHocVan"].ToString();

                    if (Rows["Active"].ToString() == null || Rows["Active"].ToString() == string.Empty)
                    {
                        Obj.Active = false;
                    }
                    else
                    {
                        Obj.Active = Convert.ToBoolean(Rows["Active"]);
                    }
                    Ctr.Insert(Obj);
                }
                if (Rows.RowState == DataRowState.Modified)
                {
                    Obj = new HocVanInfo();
                    Obj.HocVanID = Rows["HocVanID"].ToString();
                    Obj.TenHocVan = Rows["TenHocVan"].ToString() == null ? string.Empty : Rows["TenHocVan"].ToString();
                    if (Rows["Active"].ToString() == null || Rows["Active"].ToString() == string.Empty)
                    {
                        Obj.Active = false;
                    }
                    else
                    {
                        Obj.Active = Convert.ToBoolean(Rows["Active"]);
                    }
                    Ctr.Update(Obj);
                }
                this.gridControl1.DataSource = Ctr.GetList();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                btnDelete.PerformClick();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.gridView1.SelectedRowsCount > 0)
            {
                string strHocVanID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "HocVanID").ToString();
                string strTenHocVan = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TenHocVan").ToString();
                if (MessageBox.Show(string.Format("Bạn có muốn xóa Học Vấn {0} không?", strTenHocVan), "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Ctr.Delete(strHocVanID);
                    this.gridControl1.DataSource = Ctr.GetList();
                }
            }
        }
    }
}
